package com.caipin.controller;

/**
 * @Author: zhunian0322@163.com
 * @DateTime: 2021-09-30 14:59
 * @Description: TODO
 */
@RestController
@RequestMapping({"/temperatureStatisticalResult"})
public class TemperatureStatisticalResultController {
    @Autowired
    private TemperatureStatisticalResultService temperatureStatisticalResultService;

    @GetMapping({"/studentTemperatureStatisticalResultList"})
    public Object getStudentTemperatureStatisticalResultList(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize, @RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime, @RequestParam(value = "grade", required = false) String grade, @RequestParam(value = "clbum", required = false) String clbum) {
        PageBean<StudentTemperature> pageBean = null;
        pageBean = this.temperatureStatisticalResultService.getStudentTemperatureStatisticalResultList(pageNum, pageSize, startTime, endTime, grade, clbum);
        return ResultJson.ok(pageBean);
    }

    @PutMapping({"/studentTemperatureStatisticalResult"})
    public Object updateStudentTemperatureStatisticalResult(@RequestBody StudentTemperature studentTemperature) {
        this.temperatureStatisticalResultService.updateStudentTemperatureStatisticalResult(studentTemperature);
        return ResultJson.ok();
    }

    @GetMapping({"/exportStudentTemperatureStatisticalResult"})
    public void exportStudentTemperatureStatisticalResult(@RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, @RequestParam(value = "grade", required = false) String grade, @RequestParam(value = "clbum", required = false) String clbum, HttpServletResponse response) {
        String filename = this.temperatureStatisticalResultService.getStudentTemperatureStatisticalResultListFile(startTime, endTime, grade, clbum);
        File file = new File(filename);
        if (file.exists()) {
            response.setContentType("application/force-download");
            response.addHeader("Content-Disposition", "attachment;fileName=" + filename);
            byte[] buff = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                ServletOutputStream outputStream = response.getOutputStream();
                int size = bis.read(buff);
                while (-1 != size) {
                    outputStream.write(buff, 0, size);
                    size = bis.read(buff);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fis != null)
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                if (bis != null)
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                try {
                    FileUtils.forceDelete(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

